package com.qlylkj.lbk.ui.search

import android.view.View
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.forest.core.etx.getCompatColor
import com.forest.core.etx.startKtxActivity
import com.forest.core.etx.toast
import com.jeremyliao.liveeventbus.LiveEventBus
import com.qlylkj.lbk.R
import com.qlylkj.lbk.base.BaseVMActivity
import com.qlylkj.lbk.model.bean.Company
import com.qlylkj.lbk.model.event.LiveEvent
import com.qlylkj.lbk.ui.company.CompanyInfoActivity
import kotlinx.android.synthetic.main.activity_search.*

class SearchActivity : BaseVMActivity<SearchViewModel>() {

    companion object{
        const val SELECT_COMPANY = "selectCompany"
    }

    private var keyword = ""

    private var lastKeyword = ""

    private var selectCompany = false

    override fun providerVMClass() = SearchViewModel::class.java

    override fun setLayoutId() = R.layout.activity_search

    private val searchAdapter by lazy {
        SearchAdapter()
    }

    override fun initView() {
        searchCompanyRecyclerView.apply {
            layoutManager = LinearLayoutManager(this@SearchActivity)
            adapter = searchAdapter.apply {
                setOnItemClickListener { adapter, _, position ->
                    val searchCompany = adapter.getItem(position) as? Company
                    searchCompany?.apply {
                        if(selectCompany){
                            when(bindingStatus){
                                -1 -> {
                                    LiveEventBus.get(LiveEvent.SELECT_COMPANY_KEY).post(this)
                                    finish()
                                }
                                0 -> {
                                    toast("该公司认证中,请重新选择未认证公司")
                                }
                                1 -> {
                                    toast("该公司已认证,请重新选择未认证公司")
                                }
                            }
                        }else{
                            startKtxActivity<CompanyInfoActivity>(
                                values = mutableListOf(
                                    CompanyInfoActivity.COMPANY_ID to id,
                                    CompanyInfoActivity.COMPANY_NAME to companyName
                                )
                            )
                        }
                    }
                }
            }
        }
        backImage.setOnClickListener(onClickListener)
        searchText.setOnClickListener(onClickListener)
        searchView.apply {
            setIconifiedByDefault(false)
            setOnQueryTextListener(onQueryTextListener)
            val textView = findViewById<SearchView.SearchAutoComplete>(R.id.search_src_text)
            textView.textSize = 14f
            textView.setHintTextColor(getCompatColor(R.color.text_content))
        }
    }

    override fun initData() {
        selectCompany = intent.getBooleanExtra(SELECT_COMPANY,false)
    }

    override fun startObserve() {
        super.startObserve()
        mViewModel.apply {
            uiState.observe(this@SearchActivity, Observer { uiModel ->
                uiModel?.run {
                    searchAdapter.setKeyword(lastKeyword)
                    searchAdapter.replaceData(if(selectCompany) companyList.filter { it.bindingStatus == -1 }.toMutableList() else companyList)
                }
            })
        }
    }

    private val onClickListener = View.OnClickListener {
        when (it.id) {
            R.id.backImage -> finish()
            R.id.searchText -> search(keyword)
            else -> {
            }
        }
    }

    private val onQueryTextListener = object : SearchView.OnQueryTextListener {

        override fun onQueryTextChange(newText: String?): Boolean {
            newText?.let {
                keyword = it
            }
            return false
        }

        override fun onQueryTextSubmit(query: String?): Boolean {
            query?.let {
                search(query)
            }
            return true
        }
    }

    private fun search(keyword: String) {
        searchView.clearFocus()
        if (lastKeyword != keyword) {
            lastKeyword = keyword
            mViewModel.search(keyword)
        }
    }


}